<?php

require_once 'IFXField.class.php';

/**
 * 	IntuiFormX Class
 *	@category IntuiWORX
 * 	@package IntuiFormX
 *	@author Jeremy Clifton <j.clifton@intuiworx.com>
 *	@copyright 2004-2007
 *	@version 4.0.4 (2007-07-17)
 */
class IntuiFormX
{
	
	/**
	 *	Associative array that holds all the defined IntuiFormField objects.
	 *	@access private
	 *	@var array
	 */
	private $fieldObjects = array();
	
	/**
	 *	Associative array for keeping track of CSS properties to be applied to 
	 *		the form itself.
	 *	@access private
	 *	@var array
	 */
	private $css = array();
	
	/**
	 *	Associative array for keeping track of JavaScript event handlers 
	 *		defined for the form.
	 *	@access private
	 *	@var array
	 */
	private $handlers = array();
	
	/**
	 *	The raw XML the form was built from.
	 *	@access private
	 *	@var string (false if no XML used)
	 */
	private $xml = false;
	
	/**
	 *	Constructor
	 */
	public function __construct() {  }
	
	/**
	 *	Adds a form field to the IntuiForm object.
	 *	@param string $name -- The DOM  id that will be assigned to this form 
	 *		field.
	 *	@param string $type -- One of: button, checkbox, file, hidden, 
	 *		password, radio select, submit, text, or textarea.
	 *	@return object -- The IntuiFormField object.
	 */
	public function addField($id, $type)
	{
		$object = new IFXField($this, $id, $type);
		$this->fieldObjects[$id] = $object;
		return $object;
	}
	
	/**
	 *	Builds the form objects from the specified XML data.
	 *	@param string $xml
	 */
	public function buildFromXML($xml) 
	{
		$this->xml = $xml;
		$sx = simplexml_load_string($xml);
		foreach ($sx->field as $f) {
			$fld = IFXField::buildFromXML($this, $f->asXML());
		}
	}
	
	/**
	 *	Builds the form from PHP templated XML.
	 *	@param string $path
	 *	@param array $vars
	 */
	public function buildFromXMLTemplate($path, $vars = array())
	{
		if (!file_exists($path)) {
			throw new Exception("IntuiForm XML file does not exist!");
		}
		extract($vars, EXTR_SKIP);
		ob_start();
		include $path;
		$xml = ob_get_contents();
		$this->buildFromXML($xml);
		ob_end_clean();
		return $xml;
	}
	
	/**
	 *	Returns an object reference for the specified field object.
	 *	@return object
	 */
	public function getField($name) { return $this->fieldObjects[$name]; }
	
	/**
	 *	Retrieves the label for a form field.
	 *	@param string $field
	 *	@return string
	 */
	public function getFieldLabel($field) 
	{ 
		$obj	=& $this->getField($field);
		return $obj->label; 
	}
	
	/**
	 *	Returns the raw XML used to build the form.
	 *	@return bool or string (false if no xml was used)
	 */
	public function getXML() { return $this->xml; }
	
	/**
	 *	Checks to see if a field is indicated as required.
	 *	@param $field
	 *	@return boolean
	 */
	public function isRequired($field)
	{
		$obj = $this->getField($field);
		return $obj->isDefault();
	}
	
	/**
	 *	Renders the specified field.
	 *	@param string $name
	 *	@return string
	 */
	public function renderField($name) 
	{ 
		$obj = $this->fieldObjects[$name];
		if (!is_object($obj)) {
			trigger_error('Could not find a field object named "'.$name.'"!', E_USER_WARNING);
			return;
		}
		return $obj->render() ;
	}
	
	/**
	 *	Renders the specified field's label.
	 *	@param string $name
	 *	@return string
	 */
	public function renderLabel($name)
	{
		$obj = $this->fieldObjects[$name];
		if (!is_object($obj)) {
			trigger_error('Could not find a field object named "'.$name.'"!', E_USER_WARNING);
			return;
		}
		return $obj->getLabel();
	}
	
	/**
	 *	Returns the CSS info for the form, all fieldsets, and all form fields.
	 *	@return string -- The CSS code.
	 */
	public function writeCSS()
	{
		$css .= "<!-- *** CSS Generated by IntuiFormX for PHP (http://www.intuiworx.com/software/IntuiFormX) *** -->\n";
		$css .= '<style type="text/css">'."\n";
		// CSS for each form field.
		if (sizeof($this->fieldObjects) > 0) {
			foreach ($this->fieldObjects as $fn => $obj) {
				$f =& $this->fieldObjects[$fn];
				if (sizeof($f->getCSSArray()) > 0) {
					$css .= "\t#".$f->getID().' { ';
					foreach ($f->getCSSArray() as $key => $val) {
						$css .= $key.': '.$val.'; ';	
					}
					$css .= "}\n";
				}
			}
		}
		$css .= '</style>'."\n";
		$css .= "<!-- *** End Generated CSS Code *** -->\n";
		return $css;
	}
	
	/**
	 *	Generates JavaScript code to assign handlers to field events.
	 *	@return string
	 */
	public function writeJavaScript()
	{
		$js .= "<!-- *** JavaScript Generated by IntuiFormX for PHP (http://www.intuiworx.com/software/IntuiFormX) *** -->\n";
		$js	.= '<script type="text/javascript">'."\n";
		// Handlers for each form field.
		foreach ($this->fieldObjects as $name => $obj) {
			if (is_object($obj)) {
				foreach ($obj->getHandlerArray() as $handler => $code) {
					$js	.= "\tEvent.observe('".$name."', '".$handler."', function(event) { ".$code." });\n";
				}
			}
		}
		$js .= "</script>\n";
		$js .= "<!-- *** End Generated JavaScript Code *** -->\n";
		return $js;
	}

}

?>